/**
 * modalProvider module
 */

 function Modal(options){
    this.options = $.extend({}, this.defaults, options);
 }
 Modal.prototype = {
     defaults: {
         addClass: '',
         appendTo: 'body',
         width: 'auto',
         height: 'auto',
         position: 'center',
         shift: {
             left: 0,
             top: 0
         },
         vitrage: true,
         closeButton: true,
         isShow: true,
         template: null,
         templateUrl: null,
         controller: null,
         scope: null,
         selectable: false,
     },
     construct: function() {
         this.element = $('<dialog>');
         this.element.addClass('btg-modal modal-constructing');
         this.element.addClass(this.options.addClass);
         this.element.attr('ng-show', 'modal.isShow');
         this.element.attr("open", "true");
         if (!this.options.selectable) {
             this.element.attr('onselectstart', 'return false');
         }
         if (this.options.vitrage) {
             this.vitrage = $('<del>');
             this.vitrage.addClass('modal-vitrage');
         } else {
             this.vitrage = $();
         }
         this.defineScope();
         this.addController();
         this.addContent();
         this.addCloseButton();
         this.vitrage.appendTo('body');
         this.element.appendTo('body');
         //this.$compile(this.element)(this.$scope);
         setTimeout(function (){
             this.vitrage.addClass('modal-vitrage-show');
             this.element.removeClass('modal-constructing');
             this.position();
         }.bind(this), 150);
         //this.$rootScope.inModal = true;
     },
     destruct: function() {
         //this.$scope.modal = null;
         this.element.remove();
         this.vitrage.addClass('modal-vitrage-hide');
         setTimeout(this.vitrage.remove.bind(this.vitrage), 10);
         //this.$rootScope.inModal = false;
     },
     hide: function() {
         //this.$scope.isShow = false;
         this.element.hide();
         this.vitrage.addClass('modal-vitrage-hide');
     },
     show: function() {
         this.element.show();
         this.vitrage.removeClass('modal-vitrage-hide');
         this.vitrage.addClass('modal-vitrage-show');
     },
     defineScope: function() {
         var modal = {
             isShow: this.options.isShow,
             position: this.position.bind(this),
             destruct: this.destruct.bind(this),
             close: this.destruct.bind(this)
         };
        //  this.$scope = this.options.scope || this.$rootScope.$new(true);
        //  this.$scope.modal = angular.extend(modal, this.$scope.modal || {});
        //  this.$scope.$watch('modal.loading', this.position.bind(this));
     },
     addCloseButton: function() {
         if (this.options.closeButton) {
             this.closeButton = $('<a>').addClass('modal-close icon-close').html('');
            //  this.closeButton.attr('ng-click', 'modal.close()');
            //  this.closeButton.attr('ng-if', '!modal.confirmClose');
             this.closeButton.appendTo(this.element);
         }
     },
     getPositionFn: function(position) {
         if (!position) {
             return function() {
                 return {};
             };
         } else if (typeof position === 'function') {
             return position;
         } else if (position === 'center') {
             return function(width, height, shift) {
                 return {
                     left: '50%',
                     top: '40%',
                     marginLeft: width * -0.5 + shift.left,
                     marginTop: height * -0.4 + shift.top
                 };
             }.bind(this);
         } else if (position === 'top') {
             return function(width, height, shift) {
                 return {
                     left: '50%',
                     top: '10%',
                     marginLeft: width * -0.5 + shift.left,
                     marginTop: height * -0.4 + shift.top
                 };
             }.bind(this);
         } else if (typeof position === 'string' || position instanceof jQuery) {
             return function(width, height, shift) {
                 var element = $(position).assert(),
                     offset = element.offset();
                 return {
                     left: offset.left + element.width() + shift.left,
                     top: offset.top + element.height() + shift.top
                 };
             };
         } else if (typeof position === 'object') {
             return function() {
                 return position;
             };
         }
     },
     position: function() {
         var position, positionFn = this.getPositionFn(this.options.position);
         this.element.css({
             position: 'absolute',
             width: this.options.width,
             height: this.options.height
         });
         position = positionFn(this.element.width(), this.element.height(), this.options.shift);
         this.element.css(position);
     },
     addController: function() {
         if (this.options.controller) {
             this.element.attr('ng-controller', this.options.controller);
         }
     },
     addContent: function() {
         this.content = $('<div>').addClass('btg-modal-content');
         if (this.options.template) {
             this.content.html(this.options.template);
         } else if (this.options.templateUrl) {
            //  this.$scope.modal.loading = true;
            //  this.content.attr('ng-class', '{loading: modal.loading}');
            //  this.content.attr('ng-include', '"' + this.options.templateUrl + '"');
            //  this.content.attr('onload', 'modal.loading=false');
         }
         this.element.append(this.content);
     }
 };
